using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLayersExample.DataAccess.Interface;

namespace NLayersExample.BusinessLogic
{
    public class BusinessLogicAccessPoint : IBusinessLogicAccessPoint
    {
        private IDataAccessAccessPoint _dataAccessAccessPoint;

        public BusinessLogicAccessPoint(IDataAccessAccessPoint dataAccessAccessPoint)
        {
            _dataAccessAccessPoint = dataAccessAccessPoint;
        }

        private Dictionary<Type, BaseBL> _instances = new Dictionary<Type, BaseBL>();

        private BaseBL GetSharedInstance<T>()
        {
            if (!_instances.ContainsKey(typeof(T)))
            {
                _instances[typeof(T)] = Activator.CreateInstance<T>() as BaseBL;

                _instances[typeof(T)].DataAccess = _dataAccessAccessPoint;
                _instances[typeof(T)].BusinessLogic = this;
            }

            return _instances[typeof(T)];
        }

        public SampleBL SampleBL
        {
            get
            {
                return GetSharedInstance<SampleBL>() as SampleBL;
            }
        }
   
        public NewBL NewBL
        {
            get
            {
                return GetSharedInstance<NewBL>() as NewBL;
            }
        }
    }
}